print("202501141725 - 打印 ReplicatedStorage.Data.PetRegistry.PetList 全量数据")

local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function log(s)
	local line = string.format("[%s] %s", os.date("%Y-%m-%d %H:%M:%S"), tostring(s))
	print("[PetList] " .. tostring(s))
	local ok = pcall(function()
		appendfile("pet_list_log.txt", line .. "\n")
	end)
	if not ok then
		pcall(function()
			writefile("pet_list_log.txt", line .. "\n")
		end)
	end
end

local function safeToString(v)
	local ok, res = pcall(function() return tostring(v) end)
	return ok and res or "[unstringifiable]"
end

local function deepCopySample(tbl, depth, maxDepth, maxItems)
	depth = depth or 0
	maxDepth = maxDepth or 6
	maxItems = maxItems or 20000
	if typeof(tbl) ~= "table" then return tbl end
	if depth >= maxDepth then return "[maxDepth]" end
	local out = {}
	local count = 0
	for k, v in pairs(tbl) do
		count = count + 1
		if count > maxItems then
			out["__truncated__"] = true
			break
		end
		if typeof(v) == "table" then
			out[safeToString(k)] = deepCopySample(v, depth + 1, maxDepth, maxItems)
		else
			out[safeToString(k)] = v
		end
	end
	return out
end

local function findPath(root, segments)
	local node = root
	for _, name in ipairs(segments) do
		node = node:FindFirstChild(name)
		if not node then return nil, "未找到: " .. table.concat(segments, ".") end
	end
	return node
end

local function exportJson(filename, data)
	local okJson, encoded = pcall(function() return HttpService:JSONEncode(data) end)
	if okJson then
		pcall(function()
			writefile(filename, encoded)
			log("✅ 已导出 " .. filename)
		end)
	else
		log("⚠️ JSON 编码失败: " .. tostring(filename))
	end
end

local function exportText(filename, lines)
	pcall(function()
		writefile(filename, table.concat(lines, "\n"))
		log("✅ 已导出 " .. filename)
	end)
end

local function requireModule(modScript)
	local ok, mod = pcall(function() return require(modScript) end)
	if not ok then return false, mod end
	return true, mod
end

local function exportFromModuleScript(modScript)
	local ok, mod = requireModule(modScript)
	if not ok then
		log("❌ require失败: " .. safeToString(mod))
		return false
	end
	local snapshot
	if typeof(mod) == "table" then
		snapshot = deepCopySample(mod, 0, 10, 50000)
	else
		snapshot = { __warn = "module returned non-table", kind = typeof(mod), value = safeToString(mod) }
	end
	exportJson("pet_list.json", snapshot)
	return true
end

local function exportMapOfModules(folder)
	-- 某些项目将 PetList 作为多个 ModuleScript 的集合
	local out = {}
	local keys = {}
	for _, child in ipairs(folder:GetChildren()) do
		if child:IsA("ModuleScript") then
			local ok, mod = requireModule(child)
			if ok then
				out[child.Name] = (typeof(mod) == "table") and deepCopySample(mod, 0, 10, 10000) or { kind = typeof(mod), value = safeToString(mod) }
				keys[#keys+1] = child.Name
			else
				out[child.Name] = { error = safeToString(mod) }
			end
		end
	end
	exportJson("pet_list_modules.json", out)
	local lines = { "=== PetList 模块子项 ===" }
	table.sort(keys)
	for i, k in ipairs(keys) do
		lines[#lines+1] = string.format("%d. %s", i, k)
	end
	exportText("pet_list_modules_keys.txt", lines)
	return true
end

local function exportInstanceTree(node)
	local function toTree(inst, depth, maxDepth, maxKids)
		depth = depth or 0
		maxDepth = maxDepth or 8
		maxKids = maxKids or 20000
		local t = { name = inst.Name, className = inst.ClassName, children = {} }
		if depth >= maxDepth then return t end
		local count = 0
		for _, c in ipairs(inst:GetChildren()) do
			count = count + 1
			if count > maxKids then break end
			t.children[#t.children+1] = toTree(c, depth + 1, maxDepth, maxKids)
		end
		return t
	end
	exportJson("pet_list_tree.json", toTree(node, 0, 10, 50000))
	return true
end

local function main()
	log("🚀 开始打印 ReplicatedStorage.Data.PetRegistry.PetList ...")
	local node, err = findPath(ReplicatedStorage, { "Data", "PetRegistry", "PetList" })
	if not node then
		log("❌ " .. tostring(err))
		return false
	end
	if node:IsA("ModuleScript") then
		return exportFromModuleScript(node)
	elseif node:IsA("Folder") then
		-- 可能是一个由多个模块组成的列表
		return exportMapOfModules(node)
	else
		-- 其他实例类型，导出结构树
		log("ℹ️ 目标不是ModuleScript/Folder，作为实例树导出")
		return exportInstanceTree(node)
	end
end

main()
